/**
 * Created with IntelliJ IDEA.
 * Description:synchronized修饰方法
 * User: lenovo
 * Date: 2022-09-22
 * Time: 20:07
 */
class Counter{
    public  int count = 0;
    public synchronized void increase(){//synchronized 上锁
        count++;
    }
}
public class Demo1 {
        public static Counter c= new Counter();
        // 搞俩线程, 每个线程都针对这个 counter 来进行 5w 次自增!!
        // 预期结果 10w !!!
        public static void main(String[] args) throws InterruptedException {
            Thread thread1 = new Thread(()->{
                for (int i = 0; i < 50000; i++) {
                    c.increase();
                }
            },"这是线程1");

            Thread thread2 = new Thread(()->{
                for (int i = 0; i < 50000; i++) {
                    c.increase();
                }
            },"这是线程2");

            thread1.start();
            thread2.start();
            //main线程阻塞,等待thread1和thread2执行完成
            thread1.join();
            thread2.join();
            //System.out.println(thread1.getState());
            System.out.println("count增加的次数:"+c.count);
        }
    }

